package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.pojo.Blog;
import com.itheima.pojo.BlogInfo;
import com.itheima.service.ESManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/search")
public class ESManagerController {

    @Autowired
    private ESManagerService esManagerService;

    @PostMapping("/initElasticSearchIndex")
    public Result initElasticSearchIndex(){
        esManagerService.initElasticIndex();
        return new Result("success","创建索引和映射成功");
    }

    @PostMapping("/importAll")
    public Result importAll(){
        esManagerService.importAll();
        return new Result("success","导入数据成功");
    }

    @PostMapping("/addElasticSearchIndexByUid")
    public Result addElasticSearchIndexByUid(@RequestParam("uid") String uid){
        esManagerService.importById(uid);
        return new Result("success","根据id导入数据成功");
    }

    @PostMapping("/deleteElasticSearchByUid")
    public Result deleteElasticSearchByUid(@RequestParam("uid") String uid){
        esManagerService.deleteById(uid);
        return new Result("success","删除ES数据成功");
    }

    @PostMapping("/deleteElasticSearchByUids")
    public Result deleteElasticSearchByUids(@RequestParam("uids") String uids){
        esManagerService.deleteByIds(uids);
        return new Result("success","批量删除ES数据成功");
    }

    @GetMapping("/elasticSearchBlog")
    public Result elasticSearchBlog(String keywords,Integer currentPage,Integer pageSize){
        List<BlogInfo> blogInfoList=esManagerService.findByKeywords(keywords,currentPage,pageSize);
        return new Result("success",blogInfoList);
    }

    @GetMapping("/sqlSearchBlog")
    public Result sqlSearchBlog(String keywords,Integer currentPage,Integer pageSize){
        Page<BlogInfo> blogInfoList=esManagerService.find2BlogByKeywords(keywords,currentPage,pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("total",blogInfoList.getTotalElements());
        map.put("totalPage",blogInfoList.getTotalPages());
        map.put("pageSize",pageSize);
        map.put("currentPage",currentPage);
        map.put("blogList",blogInfoList.getContent());
        return new Result("success",map);
    }

    @GetMapping("/searchBlogByTag")
    public Result searchBlogByTag(String tagUid,Integer currentPage,Integer pageSize){
        List<BlogInfo> blogInfoList=esManagerService.findByTagUid(tagUid,currentPage,pageSize);
        return new Result("success",blogInfoList);
    }

    @GetMapping("/searchBlogBySort")
    public Result searchBlogBySort(String blogSortUid,Integer currentPage,Integer pageSize){
        List<BlogInfo> blogInfoList=esManagerService.findByBlogSortUid(blogSortUid,currentPage,pageSize);
        return new Result("success",blogInfoList);
    }

    @GetMapping("/searchBlogByAuthor")
    public Result searchBlogByAuthor(String author,Integer currentPage,Integer pageSize){
        List<BlogInfo> blogInfoList=esManagerService.findByAuthor(author,currentPage,pageSize);
        return new Result("success",blogInfoList);
    }
}
